package per.com.utils;

import com.google.gson.Gson;

/**
 * @author zxy
 */
public class GsonUtils {

   private static Gson gson = null;
   static {
      if (gson == null){
         gson = new Gson();
      }
   }

   private GsonUtils(){

   }

   /**
    * 将对象转化为 Json
    * @param object
    * @return
    */
   public static String objectToGson(Object object){
      String gsonString = null;
      if (gson != null){
         gsonString = gson.toJson(object);
      }
      return gsonString;
   }

   /**
    * 将 Json 转化为 String
    * @param gsonString
    * @param cls
    * @param <T>
    * @return
    */
   public static <T> T gsonToBean(String gsonString, Class<T> cls) {
      T t = null;
      if (gson != null) {
         t = gson.fromJson(gsonString, cls);
      }
      return t;
   }
}
